// pages/my/address/address.js
var app = getApp()
Page({
  data: {
    addressList:[
    // {
    //   name:"朱凯里",
    //   phone:"18543286754",
    //   address:"江苏省高新区运河路47号苏州新大洲维修一分店",
    //   checked:true
    // }
    ],
    type:''//1是从订货结算页跳转
  },
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
      this.getAddressList(res.data.agentId)
    })
  },
  bindSelect(e){
    let i = e.currentTarget.dataset.index
    
    let addressList = this.data.addressList
    addressList.forEach(item=>{
      item.checked = false
    })
    this.setData({
      addressList
    })
    this.setData({
      ['addressList['+i+'].checked']:true
    })

    if (this.data.type==1) {    
      let pages = getCurrentPages()
      let prev = pages[pages.length - 2]
      prev.setData({
        name:addressList[i].name,
        phone:addressList[i].phone,
        address:addressList[i].address,
        addressId:addressList[i].addressId
      })
      wx.navigateBack({
        delta:1
      })
    }
  },
  getAddressList(agentId){
    var that = this
    var data = {
      agentId
    }
    let addressList = this.data.addressList
    app.request('wx_app/agent/address/list',data,'GET').then(res=>{
      console.log(res)
      res.data.forEach(item=>{
          addressList.push({
            name:item.contactName,
            phone:item.mobilePhone,
            address:item.address,
            checked:false,
            addressId:item.id
          })
      })
      that.setData({
        addressList
      })
    })
  }
})